﻿using DataScripter.Excel.Formatters;
using DataScripter.Excel.Formulas;

namespace DataScripter.Excel.FormattedValues
{
	class Number : BaseFormattedValue
	{
		private int? _nrOfDecimals;
		public int? NrOfDecimals
		{
			get { return _nrOfDecimals; }
			set { 
				_nrOfDecimals = value; 
				if (Formatter is NumberFormatter)
				{
					Formatter = new NumberFormatter(_nrOfDecimals);
				}
			}
		}

		private readonly decimal? _value;
		private readonly Formula _formula;

		public Number(decimal? value) : this(2)
		{
			_value = value;
		}
		public Number(Formula formula) : this(2)
		{
			_formula = formula;
		}

		public Number(Formula formula, int nrOfDecimals) : this(nrOfDecimals)
		{
			NrOfDecimals = nrOfDecimals;
			_formula = formula;
		}

		private Number(int nrOfDecimals)
			: base(new NumberFormatter(nrOfDecimals))
		{
		}

		#region Implementation of IFormattedValue

		public override object Value
		{
			get { return _value.HasValue ? (object) _value.Value : _formula; }
		}

		#endregion
	}
}
